package csci318.sfm.g3.fetest;

import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.fxml.JavaFXBuilderFactory;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class Csci318 extends Application {

    private Stage stage;
    private User loggedUser;
    private static Csci318 instance;

    public Csci318() {
        instance = this;
    }

    public static Csci318 getInstance() {
        return instance;
    }

    @Override
    public void start(Stage primaryStage) {
        try {
            stage = primaryStage;
//            gotoLogin();
            gotoDatabase();
            primaryStage.show();
        } catch (Exception ex) {
            Logger.getLogger(Csci318.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public static void main(String[] args) {
        launch(args);
    }

    public User getLoggedUser() {
        return loggedUser;
    }

    public boolean adminLogging(String userId, String password) {
        if (Authenticator.validate(userId, password)) {
            loggedUser = User.of(userId);
            gotoAdminSelect();
            return true;
        } else {
            return false;
        }
    }

    public boolean staffLogging(String userId, String password) {
        if (Authenticator.validate(userId, password)) {
            loggedUser = User.of(userId);
            gotoStaffSelect();
            return true;
        } else {
            return false;
        }
    }

    public boolean userLogging(String userId, String password) {
        if (Authenticator.validate(userId, password)) {
            loggedUser = User.of(userId);
            gotoUserSelect();
            return true;
        } else {
            return false;
        }
    }

    public void userLogout() {
        loggedUser = null;
        gotoLogin();
    }

    public void gotoDatabase() {
        try {
            replaceSceneContent("Database.fxml");
        } catch (Exception ex) {
            Logger.getLogger(Csci318.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private void gotoLogin() {
        try {
            replaceSceneContent("Login.fxml");
        } catch (Exception ex) {
            Logger.getLogger(Csci318.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void gotoAdminSelect() {
        try {
            replaceSceneContent("AdminSelect.fxml");
        } catch (Exception ex) {
            Logger.getLogger(Csci318.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    private void gotoStaffSelect() {
        try {
            replaceSceneContent("StaffSelect.fxml");
        } catch (Exception ex) {
            Logger.getLogger(Csci318.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    private void gotoUserSelect() {
        try {
            replaceSceneContent("UserSelect.fxml");
        } catch (Exception ex) {
            Logger.getLogger(Csci318.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private Parent replaceSceneContent(String fxml) throws Exception {
        String path = "/csci318/sfm/g3/scene/" + fxml;
        Parent page = (Parent) FXMLLoader.load(Csci318.class.getResource(path), null, new JavaFXBuilderFactory());
        Scene scene = stage.getScene();
        if (scene == null) {
            scene = new Scene(page);
            stage.setScene(scene);
        } else {
            stage.getScene().setRoot(page);
        }
        stage.sizeToScene();
        return page;
    }
}
